package 优先算法.双指针.快乐数;

/**快乐数
 * 网址:https://leetcode.cn/problems/happy-number/submissions/530844916/
 */
public class HappyNum {
    public static void main(String[] args) {
        Solution so = new Solution();
        System.out.println(so.isHappy(19));
    }
}

class Solution {
    public boolean isHappy(int n) {
        //一定会是个环
        int slow=fun(n);
        int fast=fun2(n);
        while(slow != fast){
            slow=fun(slow);
            fast=fun2(fast);
        }
        if(slow==1 || fast==1){
            return true;
        }
        return false;
    }
    public static int fun(int n){
        int sum=0;
        while(n>0){
            sum+=Math.pow(n%10,2);
            n/=10;
        }
        return sum;
    }
    public static int fun2(int n){ //也可以fun(fun(x))，当时没想到
        int rel=fun(n);
        int sum=0;
        while(rel>0){
            sum+=Math.pow(rel % 10,2);
            rel /=10;
        }
        return sum;
    }
}